<?php

namespace app\api\controller;

use app\api\model\ImgBurn;
use app\api\model\PacketImg;
use app\api\model\RankingRecords;
use app\api\model\UserImg as UserImgModel;
use app\api\model\User;
use app\api\model\UserZan;
use app\api\model\Privatechat;
use think\Request;
use think\Db;
use think\Cache;

class Userimg extends Common
{
    public function lists(Request $request = null)
    {
        $where = ['user_id' => $this->params['user_id'], 'is_auth' => 0, 'status' => 1, 'delete_time'=>0];

        if ($request->param('slide')) {
            $where['types'] = 1;
            $where['burn'] = 0;
            $where['packet'] = 0.00;

            $ids = UserZan::where([
                'types' => 1,
                'user_id' => $this->params['to_user'],
                'to_user' => $this->params['user_id']
            ])->column('attachment_id');

            $userImg = UserImgModel::where($where)->order('is_avatar desc')->limit(10)->select();

            if (!$userImg) {
                Db::name('user_img')->insert([
                    'user_id' => $this->params['user_id'],
                    'pic' => json_encode([$this->member['head_portrait']]),
                    'picthumb' => json_encode([$this->member['head_300']]),
                    'is_avatar' => 1
                ]);

                $userImg = UserImgModel::where($where)->order('is_avatar desc')->limit(10)->select();
            }
        } else {
            $where['is_avatar'] = 0;

            $imgBurn = ImgBurn::where([
                'user_id' => $this->params['user_id'],
                'to_user' => $this->params['to_user']
            ])->column('id');

            $packetImg = PacketImg::where([
                'user_id' => $this->params['user_id'],
                'to_user' => $this->params['to_user']
            ])->column('img_id');

            $userImg = UserImgModel::where($where)->order('id desc')->select();
        }

        foreach ($userImg as $key => $item) {
            if ($request->param('slide')) {
                $userImg[$key]['type'] = 0;
                $userImg[$key]['see'] = 1;
                $userImg[$key]['is_zan'] = in_array($item['id'], $ids) ? 1 : 0;
            } else {
                $userImg[$key]['type'] = in_array($item['id'], $imgBurn) ? 0 : 1;
                $userImg[$key]['see'] = in_array($item['id'], $packetImg) ? 1 : 0;
            }

            $userImg[$key]['pic'] = json_decode($item['pic'], true);
            $userImg[$key]['picthumb'] = json_decode($item['picthumb'], true);
        }

        return $this->pass($userImg, 'result');
    }

    public function delImg()
    {
        $userImg = UserImgModel::get($this->params['img_id']);
        

        if ($userImg) {
            UserImgModel::update(['id'=>$this->params['img_id'], 'delete_time'=>time()]);
            $images = $imagesa = $imagesb = [];
            if($userImg['pic']){
                $imagesa = json_decode($userImg['pic'], true);
            }
            if($userImg['picthumb']){
                $imagesb = json_decode($userImg['picthumb'], true);
            }
            $images = array_unique(array_merge($imagesa, $imagesb));
            foreach ($images as $key => $value) {
                \app\service\AliOss::delete($value);
            }

            Cache::rm('photos' . $userImg['user_id']);
            Cache::rm('photosCount' . $userImg['user_id']);
        }
        
        return $this->return['common.success.delete'];
    }

    public function like()
    {
        $userZan = UserZan::get([
            'types' => 1,
            'user_id' => $this->params['user_id'],
            'attachment_id' => $this->params['id'],
        ]);

        if ($userZan) {
            return $this->return['user.error.notSupportCancel'];
        } else {
            $userImg = UserImgModel::get($this->params['id']);

            if (!$userImg) return $this->return['userImg.error.notExist'];

            UserZan::create([
                'user_id' => $this->params['user_id'],
                'to_user' => $userImg->user_id,
                'attachment_id' => $this->params['id'],
                'create_time' => time()
            ]);
            User::where(['id' => $userImg->user_id])->setInc('charm_value');
            User::where(['id' => $userImg->user_id])->setInc('zan');

            // 榜单记录
            //RankingRecords::index(6, $userImg->user_id, $this->params['user_id'], 1);

            $msg = $userImg->is_avatar ? '头像' : '照片';

            /*$notice = '恭喜！您的魅力吸引了' . $this->member['name'] . '的关注，Ta给您的'.$msg.'点赞啦！帮您提升了1魅力值';
            Privatechat::notice($userImg->user_id, $notice);*/

            return $this->return['user.success.like'];
        }
    }

}
